package com.club.service.impl;

import ch.qos.logback.core.pattern.Converter;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.map.MapUtil;
import com.club.common.constant.Constant;
import com.club.entity.R;
import com.club.entity.SysPub;
import com.club.mapper.SysOperationMapper;
import com.club.mapper.SysPubMapper;
import com.club.service.SystemService;
import com.club.util.DateUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author chaochao(vx : zhangfec)
 * @Date 2023/1/11 16:58
 * @Version 1.0
 */
@Service
@RequiredArgsConstructor
public class SystemServiceImpl implements SystemService {

    private final SysOperationMapper sysOperationMapper;

    private final SysPubMapper sysPubMapper;

    @Override
    public R getPubData() {
        List<SysPub> sysPubList = sysPubMapper.findAll();
        Map<String, Object> responseMap = new HashMap<>();
        responseMap.put("data", sysPubList);
        return R.ok(responseMap);
    }

    @Override
    public R getOperationData() {
        List<Map<String, Object>> operationData = sysOperationMapper.getOperationData();
        List<Map<String, Object>> collect = operationData.stream()
                .map(operation -> MapUtil.builder(new HashMap<String, Object>())
                        .put("userName", Convert.toStr(operation.get("username")))
                        .put("operaMsg", Convert.toStr(operation.get("operate_msg")))
                        .put("operaTime", DateUtil.formatDate(Convert.toDate(operation.get("create_time")), Constant.TimePlate.STAND_TIME))
                        .put("loginTime", DateUtil.formatDate(Convert.toDate(operation.get("login_time")), Constant.TimePlate.STAND_TIME))
                        .build()).collect(Collectors.toList());
        Map<String, Object> responseMap = new HashMap<>();
        responseMap.put("data", collect);
        return R.ok(responseMap);
    }
}
